﻿using System.Reflection;

namespace JobTools.Helper
{
    public class ReflectionFactory<T>
    {
        public static List<T> CreateInstance(string sFilePath, string[] typeFeature, Type hostType = null, bool dynamicLoad = true)
        {
            List<T> list = new List<T>();
            Assembly assembly = null;
            bool flag = !dynamicLoad;
            if (flag)
            {
                assembly = Assembly.LoadFrom(sFilePath);
            }
            else
            {
                using (FileStream fileStream = new FileStream(sFilePath, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader binaryReader = new BinaryReader(fileStream))
                    {
                        byte[] rawAssembly = binaryReader.ReadBytes((int)fileStream.Length);
                        binaryReader.Close();
                        fileStream.Close();
                        assembly = Assembly.Load(rawAssembly);
                    }
                }
            }
            bool flag2 = assembly != null;
            if (flag2)
            {
                Type[] types = assembly.GetTypes();
                int i = 0;
                while (i < types.Length)
                {
                    Type type = types[i];
                    try
                    {
                        bool flag3 = type.ToString().Contains("<>");
                        if (!flag3)
                        {
                            bool flag4 = typeFeature != null;
                            if (flag4)
                            {
                                bool flag5 = true;
                                for (int j = 0; j < typeFeature.Length; j++)
                                {
                                    string value = typeFeature[j];
                                    bool flag6 = type.ToString().Contains(value);
                                    if (flag6)
                                    {
                                        flag5 = false;
                                        break;
                                    }
                                }
                                bool flag7 = flag5;
                                if (flag7)
                                {
                                    goto IL_206;
                                }
                            }
                            T item = (T)((object)assembly.CreateInstance(type.ToString()));
                            list.Add(item);
                            bool flag8 = hostType == null;
                            if (flag8)
                            {
                                throw new Exception("[Demo版提示]\r\n您已成功加载了【" + type.ToString() + "】:)");
                            }
                            AssemblyInfoHelper assemblyInfoHelper = new AssemblyInfoHelper(hostType);
                        }
                    }
                    catch (InvalidCastException value2)
                    {
                        Console.WriteLine(value2);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Concat(new string[]
                        {
                            "Create ",
                            sFilePath,
                            "(",
                            type.ToString(),
                            ") occur ",
                            ex.GetType().Name,
                            ":\r\n",
                            ex.Message,
                            (ex.InnerException != null) ? ("(" + ex.InnerException.Message + ")") : ""
                        }));
                    }
                IL_206:
                    i++;
                    continue;
                    goto IL_206;
                }
            }
            return list;
        }
    }
}
